package com.wise.site.more;

import com.wise.site.R;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.utils.CommentUtils;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;
import com.wise.site.utils.CommentUtils.CommentUploadData;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

public class FeedbackActivity extends Activity implements View.OnClickListener, TextWatcher {
	private EditText mEditText;
	private TextView mTextView;
	private final static int MAXLENGTH = 140;
	//private CharSequence mText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_feedback_activity);
		((TextView)findViewById(R.id.activity_title)).setText(getString(R.string.feedback));
		findViewById(R.id.title_back).setOnClickListener(this);
		findViewById(R.id.title_send).setOnClickListener(this);
		mEditText = (EditText) findViewById(R.id.content);
		mEditText.addTextChangedListener(this);
		mTextView = (TextView) findViewById(R.id.length);
		mTextView.setText(MAXLENGTH + "");
		
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id){
		case R.id.title_back:
			finish();
			break;
		case R.id.title_send:
			LoginResult lr = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
			if(lr == null){
				sendBroadcast(new Intent("sw.login"));
				finish();
				break;
			}
			CommentUtils.getInstance().uploadComment(new CommentUploadData(0, lr.id, lr.userName, mEditText.getText().toString()), 4, null, this);
			mEditText.setText("");
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  

			break;
		}
	}

	@Override
	public void afterTextChanged(Editable editable) {
	}

	@Override
	public void beforeTextChanged(CharSequence str, int arg1, int arg2,
			int arg3) {
	}

	@Override
	public void onTextChanged(CharSequence str, int arg1, int arg2, int arg3) {
		mTextView.setText(MAXLENGTH - str.length() + "");
	}
}
